const remoteInfo = 'userinfo'

// 
function getCloudBase() {
  return getApp().globalData.cloudbase
}

async function getUserInfo() {
  const cloudbase = getCloudBase()
  const res = await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      // type: 'getShareOpenID',
      type: 'getOpenID',
    }
  })
  return res.result
}

async function getBaseConfig(gameCode, nickname) {
  const userinfo = wx.getStorageSync('userinfo')
  console.log('get config', nickname)

  const cloudbase = getCloudBase()
  const res = await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      type: 'baseConfig',
      code: gameCode,
      userinfo,
      nickname,
    }
  })

  return res
}

async function saveRemoteRecord() {
  const gameCode = wx.getStorageSync('gameCode')
  const userinfo = wx.getStorageSync('userinfo')
  const game = {
    totalCount: wx.getStorageSync('totalCount'),
    finishCount: wx.getStorageSync('finishCount'),
    leftCount: wx.getStorageSync('leftCount'),
  }
  console.log('save record', game)

  const cloudbase = getCloudBase()
  return await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      type: 'saveRecord',
      code: gameCode,
      userinfo,
      game,
    }
  })
}

async function getRemoteRecord() {
  const gameCode = wx.getStorageSync('gameCode')
  const userinfo = wx.getStorageSync('userinfo')
  console.log('get record')

  const cloudbase = getCloudBase()
  return await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      type: 'selectRecord',
      code: gameCode,
      userinfo,
    }
  })
}

async function getAdminRemoteRecord() {
  const gameCode = wx.getStorageSync('gameCode')
  const userinfo = wx.getStorageSync('userinfo')
  console.log('get admin record')

  const cloudbase = getCloudBase()
  return await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      type: 'adminRecord',
      code: gameCode,
      userinfo,
    }
  })
}

async function saveRemoteGold() {
  const gameCode = wx.getStorageSync('gameCode')
  const userinfo = wx.getStorageSync('userinfo')
  const gold = wx.getStorageSync('gold')
  console.log('save gold', gold)

  const cloudbase = getCloudBase()
  return await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      type: 'saveGold',
      code: gameCode,
      userinfo,
      gold,
    }
  })
}

async function saveRemoteGPS(gps) {
  const gameCode = wx.getStorageSync('gameCode')
  const userinfo = wx.getStorageSync('userinfo')
  console.log('save gps', gps)

  const cloudbase = getCloudBase()
  return await cloudbase.callFunction({
    name: remoteInfo,
    data: {
      type: 'saveGPS',
      code: gameCode,
      userinfo,
      gps,
    }
  })
}

function getPageConfig(page_name) {
  const page_config = wx.getStorageSync('pageConfig')
  console.log('get page config', page_config)
  if (page_config === '') {
    return false
  }

  return page_config[page_name]
}

module.exports = {
  getCloudBase,
  getUserInfo,
  getBaseConfig,
  saveRemoteRecord,
  getRemoteRecord,
  saveRemoteGold,
  saveRemoteGPS,
  getAdminRemoteRecord,
  getPageConfig,
}